package com.telecom.aop;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

public class MyInvocationHandler implements InvocationHandler {
    private Object target;
    public MyInvocationHandler(Object target)
    {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开启监控......");
        long t1=System.currentTimeMillis();
        TimeUnit.MILLISECONDS.sleep((int)(Math.random()*1000));
        Object result = method.invoke(target, args);
        System.out.println("监控结束......");
        long t2=System.currentTimeMillis();
        System.out.println("方法耗时："+(t2-t1));
        BufferedWriter bw=new BufferedWriter(new FileWriter("d:/log.log",true));
        bw.write("方法耗时："+(t2-t1));
        bw.newLine();
        bw.close();
        return result;
    }
}
